import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class Case extends InitEnd{


    public static Stream<Arguments> blogPublishMethod() {
        return Stream.of(Arguments.arguments("http://127.0.0.1:8081/blog_edit.html"));
    }

    public static Stream<Arguments> blogDetailTitle() {
        return Stream.of(Arguments.arguments("http://127.0.0.1:8081/blog_detail.html", "okkk"));
    }

    public static Stream<Arguments> blogEditContent() {
        return Stream.of(Arguments.arguments("http://127.0.0.1:8081/blog_update.html"));
    }

    @Order(1)
    @Test
    void login() throws InterruptedException {
        webDriver.get("http://127.0.0.1:8081/blog_login.html");
        webDriver.manage().window().maximize();
        sleep(2000);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(2000);
        String curUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://127.0.0.1:8081/blog_list.html", curUrl);
        String userNameBlogTitle = webDriver.findElement(By.cssSelector("#user-display")).getText();
        Assertions.assertEquals("zhangsan的博客系统", userNameBlogTitle);
        String userName = webDriver.findElement(By.cssSelector("body > div.container > div.left > div > h3")).getText();
        Assertions.assertEquals("zhangsan", userName);
    }

    @Order(2)
    @Test
    void blogListNum() {
        Integer num = webDriver.findElements(By.cssSelector(".title")).size();
        Assertions.assertNotEquals(0, num);
    }

    @Order(3)
    @ParameterizedTest
    @MethodSource("blogDetailTitle")
    void blogDetail(String expectedUrl, String expectedTitle) throws InterruptedException {
        webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();
        sleep(2000);
        String curUrl = webDriver.getCurrentUrl();
        if(curUrl.contains(expectedUrl)) {
            System.out.println("测试通过");
        }else{
            System.out.println("curUrl:" + curUrl);
            System.out.println("测试不通过");
            return;
        }
        String title = webDriver.findElement(By.cssSelector("body > div.container > div.right > div > div.title")).getText();
        Assertions.assertEquals(expectedTitle, title);
    }

    @Order(4)
    @ParameterizedTest
    @MethodSource("blogEditContent")
    void blogEdit(String expectedUrl) throws InterruptedException {
        webDriver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(1)")).click();
        sleep(2000);
        String curUrl = webDriver.getCurrentUrl();
        if(curUrl.contains(expectedUrl)) {
            System.out.println("测试通过");
        }else{
            System.out.println("curUrl:" + curUrl);
            System.out.println("测试不通过");
            return;
        }
        webDriver.findElement(By.cssSelector("#title")).clear();
        webDriver.findElement(By.cssSelector("#title")).click();
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"");
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(2000);
        webDriver.switchTo().alert().accept();
        sleep(5000);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        List<WebElement> webElementList = webDriver.findElements(By.cssSelector(".title"));
        WebElement element = webElementList.get(0);
        String title = element.getText();
        Assertions.assertEquals("自动化测试",title);
    }

    @Order(8)
    @ParameterizedTest
    @MethodSource("blogPublishMethod")
    void blogPublish(String expectedUrl) throws InterruptedException {
        webDriver.findElement(By.xpath("/html/body/div[1]/a[2]")).click();
        sleep(2000);
        String curUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals(expectedUrl, curUrl);
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试基础\"");
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"content\").value=\"自动化测试\"");
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(2000);
        webDriver.switchTo().alert().accept();
        sleep(2000);
        List<WebElement> webElementList = webDriver.findElements(By.cssSelector(".title"));
        WebElement element = webElementList.get(0);
        String title = element.getText();
        Assertions.assertEquals("自动化测试基础",title);
    }

    @Order(9)
    @Test
    void blogDelete() throws InterruptedException {
        webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();
        sleep(2000);
        webDriver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(2)")).click();
        webDriver.switchTo().alert().accept();
        sleep(2000);
        List<WebElement> webElementList = webDriver.findElements(By.cssSelector(".title"));
        WebElement element = webElementList.get(0);
        String title = element.getText();
        Assertions.assertNotEquals("自动化测试基础",title);
    }

    @Order(10)
    @Test
    void userLogout() throws InterruptedException {
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        sleep(2000);
        String curUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://127.0.0.1:8081/blog_login.html", curUrl);
        WebElement webElement = webDriver.findElement(By.cssSelector("#submit"));
        Assertions.assertNotNull(webElement);
    }

}
